var fs = require('fs')
var path = require('path')

module.exports = {
    createUserDb: function(user) {
        return {
            id: createId(),
            name: user.name,
            password: user.password,
            introduction: '这个人很懒，什么也没留下...',
            identity: 0,
            age: 0,
            sex: 1,
            fans: [],
            care: [],
            collectPost: [],
            active: 0,
            qq: '',
            email: '',
            createdAt: new Date()
        }
    },
    createPostDb: function(post) {
        return {
            id: createId(),
            handler: post.handler,
            title: post.title,
            content: post.content,
            comments: [],
            commentCount: 0,
            readCount: 0,
            collects: [],
            createdAt: new Date(),
            updatedAt: new Date()
        }
    },
    createCommentDb: function(comment) {
        return {
            id: createId(),
            handler: comment.handler,
            targeter: comment.targeter,
            content: comment.content,
            comments: [],
            agreeUser: [],
            opposeUser: [],
            createdAt: new Date()
        }
    },
    createTestDb: function(test) {
        return Object.assign(test, {
            id: createId(),
            resultCount: 0,
            createdAt: new Date()
        })
    },
    //  用户提交习题动态数据
    createActiveType1_1: function(userId, test) {
        return {
            type: 1,
            active: {
                handler: {
                    name: test.autor,
                    id: userId
                },
                activity: {
                    type: 1,
                    test: {
                        name: test.name,
                        id: test.topicId || test.id
                    }
                }
            },
            createdAt: new Date()
        }
    },
    //  用户发表帖子动态数据
    createActiveType1_2: function(post) {
        return {
            type: 1,
            active: {
                handler: {
                    name: post.handler.name,
                    id: post.handler.id
                },
                activity: {
                    type: 2,
                    post: {
                        title: post.title,
                        id: post.id
                    }
                }
            },
            createdAt: new Date()
        }
    },
    //  用户回复帖子动态数据
    createActiveType2: function(comment, postObj) {
        return {
            type: 2,
            active: {
                handler: comment.handler,
                targeter: comment.targeter,
                post: {
                    name: postObj.title,
                    id: postObj.id
                },
                content: comment.content
            },
            createdAt: new Date()
        }
    },
    //  用户提交反馈
    createActiveType3: function(handler, content) {
        return {
            type: 3,
            active: {
                handler: handler,
                content: content
            },
            createdAt: new Date()
        }
    },
    //  管理员动态
    createActiveType4: function() {

    },
    //  其他动态
    createActiveType5: function(handler, content) {
        return {
            type: 5,
            active: {
                handler: handler,
                content: content
            },
            createdAt: new Date()
        }
    },
    //  用户反馈表数据
    createSuggestDb: function(handler, content) {
        return {
            id: createId(),
            handler: handler,
            content: content,
            reply: '',
            createdAt: new Date()
        }
    },
    //  用户申请管理表数据
    createApplyDb: function(handler, introduction) {
        return {
            id: createId(),
            handler: handler,
            introduction: introduction,
            status: 0,
            access: 0,
            createdAt: new Date()
        }
    },

    /**
     * 验证路径是否存在，如果不存在，先创建路径再进行其他操作
     * @Author   memory
     * @DateTime 2017-04-08
     * @param    {string}   path     一个完整的路径
     * @param    {Function} callback 异步回调
     */
    safePathCheck: function(dirPath, fileName, callback) {
        if (typeof fileName == 'function') {
            callback = fileName
            fileName = null
        }
        //  如果不存在路径，创建路径
        if (!fs.existsSync(dirPath)) {
            //  二级目录不存在的情况 -- 最多二级
            try {
                fs.mkdirSync(dirPath)
            } catch (e) {
                var parentPath = path.join(dirPath, '../')
                fs.mkdirSync(parentPath)
                fs.mkdirSync(dirPath)
            }
        }
        //  如果存在文件名，继续检查文件是否存在，如果不存在创建一个空文件
        if (fileName) {
            var filePath = dirPath + fileName
        }

        if (fileName && !fs.existsSync(filePath)) {
            callback(false)
        } else {
            callback(true)
        }

    },
    //  检查练习题类型是否正确
    isValidTestType: function(val) {
        val = +val
        return val > 0 && val < 4
    }
}

//  创建一个随机16位id
function createId() {
    return new Date().getTime().toString(16).slice(-10) + Math.random().toString(16).slice(-6)
}